package utility;

import inventory.master.view.CategoryManagementView;
import inventory.master.view.MasterManagementMainView;
import inventory.master.view.MaterialManagementView;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.SpringLayout;
import javax.swing.table.DefaultTableModel;

public class ViewUtilities 
{	
	public static java.sql.Date convert (String temp)
	{
		SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
		
		java.util.Date date = null;
		
		try {
			
			date = sdf.parse(temp);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		java.sql.Date dates = new java.sql.Date(date.getTime());
		return dates;
	}
	
	//*********************************************************************
	public static void setList(JList list, String[] data)
	{
		
		System.out.println(list.getClass().getName());
		
		DefaultListModel dl = new DefaultListModel();
		for(int i=0;i<data.length;i++)
		{
			dl.addElement(data[i]);
		}
		list.setModel(dl);
	}
	
	//*********************************************************************
	public static String[][] ret_data(JTable tab)
	{		DefaultTableModel dm=(DefaultTableModel) tab.getModel();
	        int rowcount=dm.getRowCount();
	        int colcount=dm.getColumnCount();
	        String[][] rd=new String[rowcount][colcount];
	        for(int i=0;i<rowcount;i++)	        	
	        {
	        	for(int j=0;j<colcount;j++)
	        	{
	        		rd[i][j]= dm.getValueAt(i,j)+"";
	        	}
	        }
		return rd;
	}
	
	//*********************************************************************
	public boolean   date_val(String temp)
	{
		java.util.Date date = null;
		SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
	
		try 
		{
			date = sdf.parse(temp);
		} 
		catch (ParseException e) 
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}

	//*********************************************************************
	public static void set_comboBox(JComboBox com,String[]items)
	{
		int itemCount = com.getItemCount();

		for(int i=itemCount-1;i>-1;i--){
			com.removeItemAt(i);
		}
		for(int i=0;i<items.length;i++)
		{
			com.addItem(items[i]);
		}
	}

	//*********************************************************************
	public static void refreshTable(JTable temp)
	{
		DefaultTableModel dtm=(DefaultTableModel) temp.getModel();
	
		int rCount=dtm.getRowCount();
	
		for(int i=rCount-1;i>=0;i--)
		{
			dtm.removeRow(i);
		}
	}

	//*********************************************************************
	public static void setAutoSuggestionsComboBox(JComboBox x)
	{
		AutoCompleteJComboBoxer auto=new AutoCompleteJComboBoxer(x);
	}

	//*********************************************************************
	
	// Methods takes a jlabel or jtext and sets the date funvtionality on them
	public static void makeDateComponent(Object object)
	{
		
		JLabel label;
		JTextField jtext;
		
		if(object instanceof JLabel)
		{
			label=(JLabel) object;
			SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
			Calendar  rightNow = Calendar.getInstance();
			DatePicker myDialog = new DatePicker(new JFrame(), true, rightNow);
			rightNow = myDialog.getAnswer();
			label.setText(sdf.format(rightNow.getTime()));
			myDialog.dispose();
		}
		
		else if(object instanceof JTextField)
		{
			
			jtext=(JTextField) object;;
			SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
			Calendar  rightNow = Calendar.getInstance();
			DatePicker myDialog = new DatePicker(new JFrame(), true, rightNow);
			rightNow = myDialog.getAnswer();
			jtext.setText(sdf.format(rightNow.getTime()));
			myDialog.dispose();
		}
		
		
		
		
	}

	public static Timestamp getTime()
	{
		java.util.Date d=new java.util.Date();
		Timestamp t=new Timestamp(d.getTime());
		return t;
	}
	
}
